package com.globallogic.vacationplanner.model.helper;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;

import com.globallogic.vacationplanner.model.entity.User;
import com.globallogic.vacationplanner.model.entity.Vacation;
import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.transfer.dto.impl.VacationDto;

public class DtoHelper {
	private static Logger LOGGER = Logger.getLogger(DtoHelper.class);

	public static void convertUserToUserDto(User dbUser, UserDto userDto) {
		LOGGER.info("start to convert User to UserDto");
		userDto.setLogin(dbUser.getLogin());
		userDto.setUserName(dbUser.getUsername());
		userDto.setSurname(dbUser.getSurname());
		userDto.setEmail(dbUser.getEmail());
		LOGGER.info("convert was finished success");
	}

	public static UserDto convertUserToUserDto(User dbUser) {

		UserDto userDto = new UserDto();
		userDto.setLogin(dbUser.getLogin());
		userDto.setUserName(dbUser.getUsername());
		userDto.setSurname(dbUser.getSurname());
		userDto.setEmail(dbUser.getEmail());
		return userDto;
	}

	public static User convertUserDtoToUser(UserDto userDto) {
		LOGGER.info("start to convert UserDto to User");
		User user = new User();
		user.setLogin(userDto.getLogin());
		user.setUsername(userDto.getUserName());
		user.setSurname(userDto.getSurname());
		user.setEmail(userDto.getEmail());
		user.setPassword(userDto.getPassword());
		LOGGER.info("convert was finished success");
		return user;
	}

	public static List<VacationDto> convertVacationListToDto(
			List<Vacation> dbVacationList) {

		LOGGER.info("start to convert VacationList  to DtoList");
		List<VacationDto> vacationDtoList = new ArrayList<VacationDto>();
		for (Vacation dbVacation : dbVacationList) {
			VacationDto vacationDto = new VacationDto();

			Format formatter = new SimpleDateFormat("yyyy-MM-dd",
					Locale.ENGLISH);
			String startDate = dbVacation.getStartDate();
			String endDate = dbVacation.getEndDate();
			Date startDateD = null;
			Date endDateD = null;
			try {

				LOGGER.info("Try to parse String time to Data time type");
				startDateD = ((DateFormat) formatter).parse(startDate);
				endDateD = ((DateFormat) formatter).parse(endDate);
				vacationDto.setStartDate(startDateD);
				vacationDto.setEndDate(endDateD);
				LOGGER.info("parsing was finished success");
			} catch (ParseException e) {
				LOGGER.info("parsing was finished with exception");
			}
			LOGGER.info("start to compare date for the past condition");
			if (startDateD.getTime() < new Date().getTime()
					|| endDateD.getTime() < new Date().getTime()) {
				vacationDto.setState(true);
			} else {
				vacationDto.setState(false);
			}
			vacationDto.setDescription(dbVacation.getDescription());
			vacationDtoList.add(vacationDto);
			LOGGER.info("Dto list has been build success");
		}
		return vacationDtoList;
	}

	public static List<UserDto> convertUserList(List<User> dbUserList) {
		List<UserDto> userDtoList = new ArrayList<UserDto>();
		for (User dbUser : dbUserList) {
			UserDto userDto = new UserDto();
			userDto.setLogin(dbUser.getLogin());
			userDto.setUserName(dbUser.getUsername());
			userDto.setEmail(dbUser.getEmail());
			userDtoList.add(userDto);
		}
		return userDtoList;
	}

}
